package co.uniquindio.software.medisys.logica;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;


import co.uniquindio.software.medisys.entidades.Paciente;
import co.uniquindio.software.medisys.entidades.Usuario;

public class GestorPaciente {
	
	EntityManagerFactory emf = Persistence.createEntityManagerFactory("MedisysPU");
	
	public void guardarPaciente(Paciente paciente){
		EntityManager em = emf.createEntityManager();
		EntityTransaction et = em.getTransaction();
		et.begin();
		em.persist(paciente);
		em.flush();
		et.commit();
	}
	
	public void modificarPaciente(Paciente paciente){
		EntityManager em = emf.createEntityManager();
		EntityTransaction et = em.getTransaction();
		et.begin();
		em.merge(paciente);
		em.flush();
		et.commit();
	}
	
	public Paciente consultarPaciente(int cedula) {
		Paciente p;
		try {

			EntityManager em = emf.createEntityManager();
			p = (Paciente) em.createQuery("SELECT p FROM Paciente p WHERE p.idPaciente=:cedula")
					.setParameter("cedula", cedula).getSingleResult();
			if(p != null){
				return p;
			}else{
				return null;
			}
			
		} catch (NoResultException ex) {
			ex.printStackTrace();
			p = null;
		}
		return p;
	}
	
	public List<Paciente> seleccionarpacientes(){
		EntityManager em = emf.createEntityManager();
		return em.createQuery("SELECT p FROM Paciente p").getResultList();
	}
	

}
